﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainModule.cs" company="http://buginbox.wordpress.com/">
//   Copyright (c) GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.ComponentModel.Composition;
using System.Threading;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.MefExtensions.Modularity;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using Module.Main.View;
using Wpf.Core.Event;

namespace Module.Main
{
    /// <summary>
    /// The hello world module.
    /// </summary>
    [ModuleExport(typeof(MainModule))]
    public class MainModule : IModule
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainModule"/> class.
        /// </summary>
        [ImportingConstructor]
        public MainModule()
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            _splashMessageEvent = eventAggregator.GetEvent<SplashMessageEvent>();
        }

        /// <summary>
        /// The Splash Message Event
        /// </summary>
        private readonly SplashMessageEvent _splashMessageEvent;

        /// <summary>
        /// Gets or sets the main view model.
        /// </summary>
        /// <value>
        /// The main view model. 
        /// </value>
        [Import]
        public MainViewModel MainViewModel
        {
            get;
            set;
        }

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // Do long loading operations
            LogAction("Initialization Step 1...", () => Thread.Sleep(1000));
            LogAction("Initialization Step 2...", () => Thread.Sleep(1000));
            LogAction("Initialization Step 3...", () => Thread.Sleep(1000));

            // Open main view after
            var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            MainViewModel.InitView(regionManager);
        }

        #region Splah logging

        /// <summary>
        /// Check Symbol "√" = "\x221A"
        /// </summary>
        private const string CheckSymbol = "\x221A";

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        private void Write(string message)
        {
            _splashMessageEvent.Publish(new SplashMessageEvent { Message = message });
        }

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        private void WriteLine(string message)
        {
            _splashMessageEvent.Publish(new SplashMessageEvent { Message = message + Environment.NewLine });
        }

        /// <summary>
        /// Logs the action.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="action">
        /// The action.
        /// </param>
        private void LogAction(string message, Action action)
        {
            Write(message);
            action.Invoke();
            WriteLine(CheckSymbol);
        }

        #endregion
    }
}